package net.shopxx.action.admin;

import java.util.List;
import java.util.Set;

import net.shopxx.entity.Admin;
import net.shopxx.entity.Dept;
import net.shopxx.service.DeptService;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * 后台Action类 - 部门
 */

@ParentPackage("admin")
public class DeptAction extends BaseAdminAction {

//	private static final long serialVersionUID = -5383463207248344867L;

	/**
	 * 
	 */
	private static final long serialVersionUID = -3467508379117561835L;
	private String parentId;
	private Dept dept;
	private List<Dept> deptTreeList;
	
	@javax.annotation.Resource
	private DeptService deptService;


	// 列表
	public String list() {
		pager = deptService.findByPager(pager);
		return LIST;
	}

	// 删除
	public String delete() throws Exception{
		Dept dept = deptService.load(id);
		Set<Dept> childrenDeptSet = dept.getChildren();
		redirectionUrl = "dept!list.action";
		if (childrenDeptSet != null && childrenDeptSet.size() > 0) {
			addActionError("此部门存在下级部门，删除失败!");
			return ERROR;
		}
		Set<Admin> adminSet = dept.getAdminSet();
		if (adminSet != null && adminSet.size() > 0) {
			addActionError("此部门下存在"+adminSet.size()+"个员工，删除失败!");
			return ERROR;
		}
		deptService.delete(id);
//		return ajaxJsonSuccessMessage("删除成功！");
		return SUCCESS;
	}

	// 添加
	public String add() {
		return INPUT;
	}

	// 编辑
	public String edit() {
		dept = deptService.load(id);
		return INPUT;
	}

	// 保存
	@Validations(
			requiredStrings = { 
					@RequiredStringValidator(fieldName = "dept.deptname", message = "部门名称不允许为空!"),
				}, 
				requiredFields = { 
					@RequiredFieldValidator(fieldName = "dept.orderList", message = "排序不允许为空!")
				},
				intRangeFields = {
					@IntRangeFieldValidator(fieldName = "dept.orderList", min = "0", message = "排序必须为零或正整数!")
				}
	)
	@InputConfig(resultName = "error")
	public String save() throws Exception {
		if (StringUtils.isNotEmpty(parentId)) {
			Dept parent = deptService.load(parentId);
			dept.setParent(parent);
		} else {
			dept.setParent(null);
		}
		deptService.save(dept);
		redirectionUrl = "dept!list.action";
		return SUCCESS;
	}

	// 更新
	@Validations(
		requiredStrings = { 
			@RequiredStringValidator(fieldName = "dept.deptname", message = "部门名称不允许为空!"),
		}, 
		requiredFields = { 
				@RequiredFieldValidator(fieldName = "dept.orderList", message = "排序不允许为空!")
			},
			intRangeFields = {
				@IntRangeFieldValidator(fieldName = "dept.orderList", min = "0", message = "排序必须为零或正整数!")
			}
	)
	@InputConfig(resultName = "error")
	public String update() throws Exception {
		Dept persistent = deptService.load(id);
		BeanUtils.copyProperties(dept, persistent, new String[]{"id", "createDate", "modifyDate", "path", "parent", "children", "adminSet"});
		deptService.update(persistent);
		redirectionUrl = "dept!list.action";
		return SUCCESS;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public List<Dept> getDeptTreeList() {
		deptTreeList = deptService.getDeptTreeList();
		return deptTreeList;
	}

	public void setDeptTreeList(List<Dept> deptTreeList) {
		this.deptTreeList = deptTreeList;
	}

	public DeptService getDeptService() {
		return deptService;
	}

	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}


}